define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/index',
                    add_url: 'goods/add',
                    edit_url: 'goods/edit',
                    del_url: 'goods/del',
                    multi_url: 'goods/multi',
                    table: 'goods',
                }
            });

            var table = $("#table");
            $(".btn-add").data("area", ["100%", "100%"]);
//当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".btn-editone").data("area", ["100%", "100%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'category.name', title: __('Category_id'), operate: 'LIKE %...%'},
                        // {field: 'extend_cat_id', title: __('Extend_cat_id')},
                        // {field: 'goods_no', title: __('Goods_no')},
                        {field: 'goods_name', title: __('Goods_name'), operate: 'LIKE %...%'},
                        // {field: 'click_count', title: __('Click_count')},
                        {field: 'brand.name', title: __('Brand_id'), operate: 'LIKE %...%'},
                        {field: 'store_count', title: __('Store_count'), sortable: true},
                        // {field: 'weight', title: __('Weight')},
                        // {field: 'market_price', title: __('Market_price'), operate:'BETWEEN'},
                        {field: 'shop_price', title: __('Shop_price'), operate:'BETWEEN'},
                        // {field: 'cost_price', title: __('Cost_price'), operate:'BETWEEN'},
                        // {field: 'keywords', title: __('Keywords')},
                        // {field: 'goods_remark', title: __('Goods_remark')},
                        {field: 'original_image', title: __('Original_image'), formatter: Table.api.formatter.image},
                        // {field: 'is_virtual', title: __('Is_virtual'), visible:false, searchList: {"3) unsigne":__('3) unsigne')}},
                        // {field: 'is_virtual_text', title: __('Is_virtual'), operate:false},
                        // {field: 'virtual_indate', title: __('Virtual_indate')},
                        // {field: 'virtual_limit', title: __('Virtual_limit')},
                        // {field: 'virtual_refund', title: __('Virtual_refund'), visible:false, searchList: {"1":__('Virtual_refund 1')}},
                        // {field: 'virtual_refund_text', title: __('Virtual_refund'), operate:false},
                        {field: 'is_on_sale', title: __('Is_on_sale'), visible:false, searchList: {"1":'上架','0':'下架',"2":"审核中"}},
                        {field: 'is_on_sale_text', title: __('Is_on_sale'), operate:false},
                        // {field: 'is_free_shipping', title: __('Is_free_shipping'), visible:false, searchList: {"1) unsigne":__('1) unsigne')}},
                        // {field: 'is_free_shipping_text', title: __('Is_free_shipping'), operate:false},
                        // {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'sort', title: __('Sort'), sortable: true},
                        {field: 'is_recommend', title: __('Is_recommend'), visible:false, searchList: {"1":'是',"0":"否"}},
                        {field: 'is_recommend_text', title: __('Is_recommend'), operate:false},
                        {field: 'is_new', title: __('Is_new'), visible:false, searchList: {"1":'是',"0":"否"}},
                        {field: 'is_new_text', title: __('Is_new'), operate:false},
                        {field: 'is_hot', title: __('Is_hot'), visible:false, searchList: {"1":'是',"0":"否"}},
                        {field: 'is_hot_text', title: __('Is_hot'), operate:false},
                        // {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'give_integral', title: __('Give_integral')},
                        // {field: 'exchange_integral', title: __('Exchange_integral')},
                        // {field: 'sale_number', title: __('Sale_number')},
                        // {field: 'commission', title: __('Commission'), operate:'BETWEEN'},
                        // {field: 'spu', title: __('Spu')},
                        // {field: 'sku', title: __('Sku')},
                        // {field: 'shipping_area_ids', title: __('Shipping_area_ids')},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                // {name: 'cancel', text: '取消订单', title: '取消订单', icon: '', classname: 'btn btn-xs btn-danger btn-addtabs', url: 'order/edit',},
                            ],
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                // if(row.order_status > 2){
                                //     $(table).data("operate-cancel", null);
                                // }
                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});